/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.daoImpl;

import br.com.deployment.dao.DeterminationDAO;
import br.com.deployment.entities.Determinations;
import br.com.deployment.entities.Issues;
import br.com.deployment.entities.QuestionnaireResponse;
import br.com.deployment.entities.Response;
import br.com.deployment.genericdao.GenericDAO;
import br.com.depoyment.VO.DeterminationAux;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author ely
 */
@Stateless
@LocalBean
public class DeterminationDAOImpl extends GenericDAO<Determinations> implements DeterminationDAO {

    public DeterminationDAOImpl() {
        super(Determinations.class);
    }

    @Override
    public List<Determinations> findDeterminations(Determinations determination) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Determinations> findDeterminations(Response response, QuestionnaireResponse ques) {
        String sql = "SELECT d FROM QuestionnaireResponse q JOIN q.response r JOIN r.determination d WHERE "
                   + " q.id = :idI";
        Query query = getEntityManager().createQuery(sql);
//        query.setParameter("id", response.getId());
        query.setParameter("idI", ques.getId());
        System.err.println("Determination: "+(List<Determinations>) query.getResultList());
        return (List<Determinations>) query.getResultList();
    }
    

    @Override
    public List<DeterminationAux> findAllDeterminations() {
       String sql = "SELECT DISTINCT(q) FROM QuestionnaireResponse q JOIN q.response r JOIN r.determination d WHERE d.open='true'";
       Query query = getEntityManager().createQuery(sql);
       List<QuestionnaireResponse> questionaries = (List<QuestionnaireResponse>)query.getResultList();
       List<DeterminationAux> determinations = new ArrayList<DeterminationAux>();
       DeterminationAux aux;
       System.out.print(questionaries);
        for ( QuestionnaireResponse q : questionaries) {
            aux = new DeterminationAux();
            aux.setDate(q.getDateQ());
            aux.setDeterminations(findDeterminationsOpen(q));
            aux.setExecutor(q.getExecutor());
            aux.setNotaryOffice(q.getNotaryOffice());
            determinations.add(aux);
        }
        return determinations;
    }
    
    private List<Determinations> findDeterminationsOpen(QuestionnaireResponse ques) {
        String sql = "SELECT d FROM QuestionnaireResponse q JOIN q.response r JOIN r.determination d WHERE "
                   + " q.id = :idI AND d.open='TRUE'";
        Query query = getEntityManager().createQuery(sql);
//        query.setParameter("id", response.getId());
        query.setParameter("idI", ques.getId());
        System.err.println("Determination: "+(List<Determinations>) query.getResultList());
        return (List<Determinations>) query.getResultList();
    }
    
      public Issues findIssuesDeterminations(Determinations determinations) {
        String sql = "SELECT i FROM Response r JOIN r.issues i JOIN r.determination d WHERE "
                   + " d.id = :id AND d.open='TRUE'";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("id", determinations.getId());
        System.err.println("determi: "+query.getResultList());
        return (Issues) query.getSingleResult();
    }
}
